using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentFeeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentFeeTable objects.
    /// </summary>
    public class StudentFeeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentFeeTable from the underlying datasource.
        /// </summary>
        /// <param name="studentFeeKey">The unique StudentFeeKey of the StudentFeeTable in the database.</param>
        /// <returns>An StudentFeeTable when the StudentFeeKey was found in the database, or null otherwise.</returns>
        public static StudentFeeTable GetItem(int studentFeeKey)
        {
            StudentFeeTable myStudentFeeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@studentFeeKey", studentFeeKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentFeeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentFeeTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentFeeTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentFeeTable objects.</returns>
        public static StudentFeeTableList GetList()
        {
            StudentFeeTableList tempList = new StudentFeeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentFeeTable in the database.
        /// </summary>
        /// <param name="myStudentFeeTable">The StudentFeeTable instance to save.</param>
        /// <returns>The new StudentFeeKey if the StudentFeeTable is new in the database or the existing StudentFeeKey when an item was updated.</returns>
        public static int Save(StudentFeeTable myStudentFeeTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentFeeTable.StudentFeeKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@studentFeeKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentFeeKey", myStudentFeeTable.StudentFeeKey);
                    }
                    if (string.IsNullOrEmpty(myStudentFeeTable.StudentFeeCode))
                    {
                        myCommand.Parameters.AddWithValue("@studentFeeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentFeeCode", myStudentFeeTable.StudentFeeCode);
                    }
                    if (myStudentFeeTable.EducationPeriodKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", myStudentFeeTable.EducationPeriodKey);
                    }
                    if (myStudentFeeTable.FeeTypeCode < 0)
                    {
                        myCommand.Parameters.AddWithValue("@feeTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@feeTypeCode", myStudentFeeTable.FeeTypeCode);
                    }
                    if (string.IsNullOrEmpty(myStudentFeeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myStudentFeeTable.Description);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentFeeTable.Status);

                    //if (myStudentFeeTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentFeeTable.CreatedDateTime);
                    //}
                    //if (myStudentFeeTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentFeeTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentFeeTable from the database.
        /// </summary>
        /// <param name="studentFeeKey">The StudentFeeKey of the StudentFeeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int studentFeeKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@studentFeeKey", studentFeeKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentFeeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentFeeTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentFeeTable myStudentFeeTable = new StudentFeeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentFeeKey")))
            {
                myStudentFeeTable.StudentFeeKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentFeeKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentFeeCode")))
            {
                myStudentFeeTable.StudentFeeCode = myDataRecord.GetString(myDataRecord.GetOrdinal("StudentFeeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodKey")))
            {
                myStudentFeeTable.EducationPeriodKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FeeTypeCode")))
            {
                myStudentFeeTable.FeeTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("FeeTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myStudentFeeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentFeeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentFeeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentFeeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentFeeTable;
        }
    }

}
